using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class vacuumMovement : MonoBehaviour
{
    public bool lowBattery = false;
    public float moveSpeed = 5f;
    private float battery = 0f;

    public Rigidbody2D rb;

    Vector2 movement;

    private Animator anim;

    void Start()
    {
        anim = GetComponent<Animator>();
        lowBattery = false;
    }
    // Update is called once per frame
    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

         if (lowBattery == true)
         {
             anim.Play("low power");
         }



    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (lowBattery == true)
        {
            anim.Play("low power");
        }

        if (collision.tag == "home base")
        {
            if (lowBattery == false)
            {
                anim.Play("idle");

            }
            else if (lowBattery == true)
            {
                anim.Play("charging");
                lowBattery = false;
                battery = 0;
                gameObject.tag = "vacuum";
            }
        }

        else if (collision.tag == "dirt")
        {   
            if (lowBattery == false && battery != 2)
                {
                    anim.Play("vc");
                    Destroy(collision.gameObject, 0.5f);
                    

                }

            battery += 1;
            
            
            if(battery == 2)
            {
                gameObject.tag = "low";
            }
            if (battery == 3)
            {
                lowBattery = true;
              

            }
            
            
        }

        
        else if (collision.tag == "background")
        {
            anim.Play("idle");
        }
    }
}

